function [a,P] = HeuristicSelect(P,S,mode)

% game parameters
global num_players_r;
global num_players_b;

% opponent parameters
global opponent;
pass = opponent.pass;
dribble = opponent.dribble;
get_open = opponent.get_open;
block = opponent.block;
go_to_ball = opponent.go_to_ball;
defensive_line = opponent.defensive_line;
offensive_line = opponent.offensive_line;

if(P{2}=='r')
    num_players = num_players_r;
    num_opponents = num_players_b;
else
    num_players = num_players_b;
    num_opponents = num_players_r;
end

if(PlayerInPossession(P,S,mode))
    B=S{1};
    B_X=B{1}(1);
    if(B_X<=offensive_line)
        [a,P] = PassOrDribble(P,S,mode);
    else
        if(rand(1)<=dribble/(pass+dribble))
            [a, P] = Dribble(P);
            SaveMemory(P,false,1,0);
        else
            k = round(rand_num(0.5,num_players+0.5)); % choose a random team-mate to pass to
            [a,P] = PassBall(P,S,mode,k);
            SaveMemory(P,true,1+k,0);
        end
    end
else
    if(TeamInPossession(P,S,mode)) % teammate is in possession
        if(rand(1)<=get_open)
            [a,P] = GetOpen(P,S,mode);
            SaveMemory(P,true,2+num_players,0);
        else % stand still
            P_num = P{3};
            [a,P] = PassBall(P,S,mode,P_num);
            SaveMemory(P,true,1+P_num,0);
        end
    else % the ball is either in the opponent, or free for the taking
        B=S{1};
        B_X=B{1}(1);
        if(B_X > defensive_line) % if the ball is behind the defensive line - go for it
            [a, P] = GoToBall(P,S,mode);
            SaveMemory(P,true,3+num_players,0);
        elseif(OpponentInPossession(P,S,mode))

            p = rand(1);

            if(p<=go_to_ball/(go_to_ball + block))
                [a, P] = GoToBall(P,S,mode);
                SaveMemory(P,true,3+num_players,0);
            else
                k = round(rand_num(0.5,num_opponents+0.5)); % choose a random opponent to block
                [a,P] = BlockPass(P,S,mode,k);
                SaveMemory(P,true,3+num_players+k,0);
            end
        else % the ball is free for the taking
            if(IsClosest(P,S,mode))
                [a,P] = GoToBall(P,S,mode);
                SaveMemory(P,true,3+num_players,0);
            elseif(rand(1) <= get_open/(get_open+block))
                [a,P] = GetOpen(P,S,mode);
                SaveMemory(P,true,2+num_players,0);
            else
                k = round(rand_num(0.5,num_opponents+0.5)); % choose a random opponent to block
                [a,P] = BlockPass(P,S,mode,k);
                SaveMemory(P,true,3+num_players+k,0);
            end
        end
    end
end

end

% function [a,P] = PassOrDribble(P,S,mode)
% % game parameters
% global num_players_r;
% global num_players_b;
% 
% if(P{2}=='r')
%     num_players = num_players_r;
%     num_opponents = num_players_b;
% else
%     num_players = num_players_b;
%     num_opponents = num_players_r;
% end
% 
% P_X=P{1}(1);
% P_Y=P{1}(2);
% P_color=P{2};
% 
% close = 0;
% 
% for l=2:numel(S) % check opponents
%     if(S{l}{2} ~= P_color)
%         Sl=S{l};
%         Sl_X=Sl{1}(1);
%         Sl_Y=Sl{1}(2);
% 
%         if(abs(Sl_X-P_X) + abs(Sl_Y-P_Y) <16 &&  abs(Sl_X-(-58)) < abs(P_X-(-58)) )    % close - do not dribble - dangerous
%             close=1;
% 
%             k = round(rand_num(0.5,num_players+0.5)); % choose a random team-mate to pass to
%             [a,P] = PassBall(P,S,mode,k);
%             SaveMemory(P,true,1+k,0);
%             break;
%         end
%     end
% end
% 
% if(~close) % just dribble
%     [a, P] = Dribble(P);
%     SaveMemory(P,false,1,0);
% end
% 
% end